//
// RSS2BLOG v0.1 URL: http://rss2blog.googlecode.com/
//
// Tool to backup all of your blog posts into local XML file using RSS.
// Currently supports blogs.mail.ru only.
// Allows to repost your local XML file back into your blog.
// Written in Windows Scripting Host JScript, port to XUL & JavaScript is in progress.
//
// Copyright (C) 2006 Maxim Golovanov <mc_seem at inbox.ru>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

var login       = "inbox/mc_seem";	// change your default blog path here
var rssFileName = "mc_seem";		// change your local RSS filename here
var numRecords  = 380;			// maximum # of records to fetch

function print(text) {
	WScript.echo(text);
}

function writeToFile(filename, text) {
	var fs   = new ActiveXObject("Scripting.FileSystemObject"); 
	var file = fs.CreateTextFile(filename, true); 
	file.WriteLine(text); 
	file.Close();
}

function readRSS(url) {
	var rssReader  = false;
	var result     = false;
	var statusCode = 0;

	try {
		rssReader = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
		// ... do nothing
	}

	if (!rssReader) {
		print("Could not create an instance of Microsoft.XMLHTTP");
		return result;
	}

	rssReader.open("GET", url, false);
	rssReader.onreadystatechange = function() {
		if (rssReader.readyState==4) {
			result     = rssReader.responseText;
			statusCode = rssReader.status;
		}
	}
	rssReader.send();

	return result;
}

function num_pad(n, pad) { 
        n = n.toString(); 
        var pd = ''; 
        while (n.length + pd.length < pad) pd += '0';
        return pd + n.toString(); 
} 

function readBlogRSS(login, rssFileName, numRecords) {
	var rssURL  = "http://blogs.mail.ru/" + login + "/?rss=1&groups=&type_filter=&skip=";
	var rssArr  = new Array();

	var srcRssXML  = new ActiveXObject("Microsoft.XMLDOM");
	var dstRssXML  = new ActiveXObject("Microsoft.XMLDOM");

	srcRssXML.async = false;
	dstRssXML.async = false;

	print("Read blog from " + rssURL);
	for (i=0; i < (numRecords/25); i++) {
		print(" - reading entries " + (i*25) + " to " + (i*25+24) + "...");
		rssText   = readRSS(rssURL + 25*i);
		rssArr[i] = rssText;

		if (i == 0) {
			dstRssXML.loadXML(rssText);
			print("  - adding first feed");
		} else {
			srcRssXML.loadXML(rssText);

			var srcItems   = srcRssXML.documentElement.getElementsByTagName("item");
			var dstChannel = dstRssXML.documentElement.getElementsByTagName("channel").item(0);

			for (j=0; j<srcItems.length; j++) {
				print("  - merging items " + i + "," + j);
				dstChannel.appendChild(srcItems.item(j));
			}
		}

//		print(" - writing to file: " + rssFileName + num_pad(i, 4) + ".rss" );
//		writeToFile(rssFileName + num_pad(i, 4) + ".rss", rssText);
	}

	dstRssXML.save(rssFileName + ".rss");

//	writeToFile(rssFileName + ".rss", dstRssXML.childNodes(0).xml);
	print("[ DONE ]");
	
	
}

readBlogRSS(login, rssFileName, numRecords);
